package com.example.tool.action;

import com.example.tool.ui.HToolIcon;
import com.example.tool.utils.FileUtil;
import com.example.tool.utils.SwingUtil;
import com.intellij.openapi.actionSystem.ActionUpdateThread;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.vfs.VirtualFileManager;
import org.jetbrains.annotations.NotNull;

import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;

/**
 * <p>Title: OutActions</p >
 * Description: 生成外部访问系统图片资源文件
 * <p>CreateTime: 2022/6/28 11:14</p>
 * @author FT
 */
public class OutActions extends AnAction {

  @Override
  public @NotNull ActionUpdateThread getActionUpdateThread() {
    return super.getActionUpdateThread();
  }

  @Override
  public void actionPerformed(@NotNull AnActionEvent e) {
    String[] configInformation = ActionUtil.getInformation(e.getProject());
    if (configInformation != null) {
      String file = "/" + configInformation[2] + "/WEB-INF/";
      SwingUtil.setTips("将会在以下路径中生成 <span style=\"color: #F56C6C;\">web.xml</span> 文件。<br><span style=\"color: #F56C6C;\">" + file + "</span>");
      ActionUtil.ActionCreatInput(e, (information, value) -> {
        String OutClassName = value.get(0);
        // xx.java
        FileUtil.generateFile(information[0] + "/" + value.get(0) + ".java", "/demo/init/resources/OutRest.txt",
            str -> str.replace("$package$", information[1])
                .replace("$ClassName$", OutClassName)
                .replace("$className$", FileUtil.lowerFirstCase(OutClassName))
                .replace("$TIME$", new SimpleDateFormat("yyyy-MM-dd").format(new Date())));

        // spring-xx-controller.xml
        String xmlPath = Objects.requireNonNull(e.getProject()).getBasePath() + "/" + configInformation[2] + "/WEB-INF/web.xml";
        // web.xml  追加写+ config
        FileUtil.generateFile(xmlPath, "/demo/base/web.txt",
            str -> {
              // 读取原本的web.xml文件删除最后一行数据，再追加写入添加的数据
              String filePath = configInformation[0] + "/ApacheJetspeed/webapps/seeyon/WEB-INF/web.xml";
              if (!FileUtil.isHave(filePath)) {
                try {
                  URL resource = new URL("file:///" + filePath);
                  String count = FileUtil.readFilePath(resource, true);
                  return count.replace("</web-app>", str.replace("$package$", information[1]) + "</web-app>");
                } catch (MalformedURLException ex) {
                  ex.printStackTrace();
                }
              }
              Messages.showMessageDialog(
                  e.getProject(),
                  "Reader OA Project web.xml FIle is Error! Please add the generated web Paste the contents of the XML file into the project web XML file.",
                  "Error",
                  HToolIcon.ModuleIcon);
              return str.replace("$package$", information[1]);
            });
        VirtualFileManager.getInstance().refreshWithoutFileWatcher(false);
        SwingUtil.setTips(null);
      }, "OutClassName: ");
    } else {
      Messages.showMessageDialog(
          e.getProject(),
          "The seeyon configuration file is required, but the corresponding file is not found or the file is incorrect, please reset it!",
          "Error",
          HToolIcon.ModuleIcon);
    }
  }

  @Override
  public void update(@NotNull AnActionEvent event) {
    ActionUtil.ActionP(event);
  }
}
